/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import agents.interfaceagent.IA_SynchCommInterface;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author Ayllu
 */
public class ChatGUI {
    
    JFrame jf_mainFrame = null;
    JLabel jl_ChatLabel = null;
    JScrollPane jsp_MessahgePane = null;
    JTextArea jta_MessageQueue = null;
    JTextField jtf_MessageToSend = null;
    JButton jb_messageButton = null;
    JPanel jp_buttonPanel;
    JPanel jp_mainPanel;
    String name;
    IA_SynchCommInterface iaAgent;

    public ChatGUI(IA_SynchCommInterface theInterfaceAgent, String name) {
        this.iaAgent = theInterfaceAgent;
        this.name=name;
    }
    
    public JButton getJb_messageButton() {
        if (jb_messageButton == null) {
            jb_messageButton = new JButton("Send");
            jb_messageButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    String messageToSend = getJtf_MessageToSend().getText();
                    getJtf_MessageToSend().setText("");
                    ChatGUI.this.iaAgent.sendWallMessage(messageToSend);
                }
            });
        }
        return jb_messageButton;
    }

    public void showFrame() {
        getJf_mainFrame().setVisible(true);
    }

    public JFrame getJf_mainFrame() {

        if (jf_mainFrame == null) {
            jf_mainFrame = new JFrame("Wall Window");
            jf_mainFrame.setSize(200, 200);
            jf_mainFrame.setContentPane(getJp_mainPanel());
        }
        return jf_mainFrame;
    }

    public JLabel getJl_ChatLabel() {
        if (jl_ChatLabel == null) {
            jl_ChatLabel = new JLabel("This is " + name);
        }
        return jl_ChatLabel;
    }

    public JPanel getJp_buttonPanel() {
        if (jp_buttonPanel == null) {
            jp_buttonPanel = new JPanel();
            jp_buttonPanel.setLayout(new GridLayout(1, 2));
            jp_buttonPanel.add(getJtf_MessageToSend());
            jp_buttonPanel.add(getJb_messageButton());
        }
        return jp_buttonPanel;
    }

    public JPanel getJp_mainPanel() {
        if (jp_mainPanel == null) {
            jp_mainPanel = new JPanel();
            jp_mainPanel.setLayout(new BorderLayout());
            jp_mainPanel.add(getJl_ChatLabel(), BorderLayout.NORTH);
            jp_mainPanel.add(getJsp_MessahgePane(), BorderLayout.CENTER);
            jp_mainPanel.add(getJp_buttonPanel(), BorderLayout.SOUTH);
        }
        return jp_mainPanel;
    }

    public JTextArea getJta_MessageQueue() {
        if (jta_MessageQueue == null) {
            jta_MessageQueue = new JTextArea("");
            jta_MessageQueue.setEditable(false);
        }
        return jta_MessageQueue;
    }

    public JTextField getJtf_MessageToSend() {
        if (jtf_MessageToSend == null) {
            jtf_MessageToSend = new JTextField();
            jtf_MessageToSend.addKeyListener(new KeyListener() {

                @Override
                public void keyTyped(KeyEvent e) {
                }

                @Override
                public void keyReleased(KeyEvent e) {
                }

                @Override
                public void keyPressed(KeyEvent e) {
                    int key = e.getKeyCode();
                    if (key == KeyEvent.VK_ENTER) {
                        getJb_messageButton().doClick();
                    }
                }
            });
        }
        return jtf_MessageToSend;
    }

    public void updateMessageQueue(String message) {
        if(!getJf_mainFrame().isVisible()){
            showFrame();
        }
        String oldText = getJta_MessageQueue().getText();
        getJta_MessageQueue().setText(oldText + (oldText.equals("") ? "" : "\n") + message);
    }

    public JScrollPane getJsp_MessahgePane() {
        if (jsp_MessahgePane == null) {
            jsp_MessahgePane = new JScrollPane(getJta_MessageQueue());
        }
        return jsp_MessahgePane;
    }

    public String getName() {
        return name;
    }
    
    
}
